﻿Imports System.IO

Public Class MainForm

    Private Sub btnAddLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLog.Click
        Dim di As New DirectoryInfo(txtFolderPath.Text)
        Dim fiAr As FileInfo() = di.GetFiles()
        Dim fi As FileInfo

        Dim oRead As StreamReader = Nothing
        Dim oWrite As StreamWriter = Nothing
        Dim inFunctionOrInSub = False
        Dim line1, line2 As String
        Dim procName As String = Nothing

        For Each fi In fiAr
            If (fi.Extension = ".cls") OrElse (fi.Extension = ".bas") Then
                fi.Attributes = FileAttributes.Normal
                Dim newFileName = fi.FullName & ".tmp"
                If File.Exists(newFileName) Then
                    File.Delete(newFileName)
                End If

                oRead = New StreamReader(fi.FullName)
                oWrite = File.CreateText(newFileName)

                Dim line As String
                line = oRead.ReadLine()

                Do While line IsNot Nothing
                    oWrite.WriteLine(line)
                    If line.StartsWith("Private Function") OrElse line.StartsWith("Public Function") Then
                        inFunctionOrInSub = True

                        Dim tempIndex = line.IndexOf(" "c)
                        Dim tempIndex2 = line.IndexOf("("c)
                        procName = fi.Name & "." & line.Substring(tempIndex + 1, tempIndex2 - tempIndex - 1)

                        Do While line IsNot Nothing AndAlso Not line.Contains(") As")
                            line = oRead.ReadLine()
                            oWrite.WriteLine(line)
                        Loop

                        oWrite.WriteLine("WriteToLogFile """ & procName & """, ""started""")
                    End If

                    If (line IsNot Nothing) AndAlso (line.StartsWith("Private Sub") OrElse line.StartsWith("Public Sub")) Then
                        inFunctionOrInSub = True

                        Dim tempIndex = line.IndexOf(" "c)
                        Dim tempIndex2 = line.IndexOf("("c)
                        procName = fi.Name & "." & line.Substring(tempIndex + 1, tempIndex2 - tempIndex - 1)

                        Do While Not line.EndsWith(")")
                            line = oRead.ReadLine()
                            oWrite.WriteLine(line)
                        Loop

                        oWrite.WriteLine("WriteToLogFile """ & procName & """, ""started""")
                    End If
                    line1 = line
                    line = oRead.ReadLine()
                    line2 = line

                    If inFunctionOrInSub AndAlso Not String.IsNullOrEmpty(line) AndAlso Not (line.Contains("End Sub") OrElse line.Contains("End Function")) AndAlso Not line1.EndsWith("_") AndAlso Not line1.Contains("Select Case") Then
                        Dim lineTemp = line.Trim().Replace("""", "^").Replace("'", "*")
                        If Not String.IsNullOrEmpty(lineTemp) Then
                            oWrite.WriteLine(" WriteToLogFile """ & procName & """ , """ & lineTemp & """ ")
                        End If

                    End If

                    If Not String.IsNullOrEmpty(line) AndAlso (line.Contains("End Sub") OrElse line.Contains("End Function")) Then
                        inFunctionOrInSub = False
                    End If
                Loop
                oWrite.Close()
                oRead.Close()

                File.Delete(fi.FullName)
                File.Move(newFileName, fi.FullName)
            End If
        Next

    End Sub
End Class
